//
//  PlanEditViewController.m
//
//
//  Created by mac on 15/1/11.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "PlanEditViewController.h"
#import "PlanEditCell.h"
#import "PlanEditBean.h"
#import "BlockEditViewController.h"
#import "SeedEditViewController.h"
#import "FertilizerEditViewController.h"
#import "DrugEditViewController.h"
#import "LPCTool.h"
#import "LPCAppColor.h"

@interface PlanEditViewController ()

@property (weak, nonatomic) IBOutlet UIButton *subBtn;

@end

@implementation PlanEditViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.subDataList1 = [[NSMutableArray alloc] initWithCapacity:10];
        self.subDataList2 = [[NSMutableArray alloc] initWithCapacity:10];
        self.subDataList3 = [[NSMutableArray alloc] initWithCapacity:10];
        self.subDataList4 = [[NSMutableArray alloc] initWithCapacity:10];
        self.dataList = [[NSMutableArray alloc] initWithObjects:self.subDataList1, self.subDataList2, self.subDataList3, self.subDataList4, nil];
        self.sectionNameList = [[NSMutableArray alloc] initWithObjects:@"地块信息", @"种子信息", @"肥料信息", @"农药信息", nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"编辑生产计划";
    if (self.type == 99) {
        self.title = @"查看生产计划";
        [self.subBtn setTitle:@"返回" forState:UIControlStateNormal];
    }
    
    [self initViews];
    [self registerNib];
}

- (void)initViews
{
    self.tblData.dataSource = self;
    self.tblData.delegate = self;
    if (self.isViewPlan)
    {
        [self addBlockInf];
        [self addSeedInf];
        [self addFertilizerInf];
        [self addDrugInf];
        [self.tblData reloadData];
    }
}

- (void)registerNib
{
    UINib *nib = [UINib nibWithNibName:@"PlanEditCell" bundle:nil];
    [self.tblData registerNib:nib forCellReuseIdentifier:@"PlanEditCell"];
}

- (void)doClickNew:(UIButton *)button
{
    UIViewController *viewController = nil;
    switch (button.tag)
    {
        case 0:
            [self addBlockInf];
            [self.tblData reloadData];
            viewController = [[BlockEditViewController alloc] initWithNibName:@"BlockEditViewController" bundle:nil];
            [self.navigationController pushViewController:viewController animated:YES];
            break;
        case 1:
            [self addSeedInf];
            [self.tblData reloadData];
            viewController = [[SeedEditViewController alloc] initWithNibName:@"SeedEditViewController" bundle:nil];
            [self.navigationController pushViewController:viewController animated:YES];
            break;
        case 2:
            [self addFertilizerInf];
            [self.tblData reloadData];
            viewController = [[FertilizerEditViewController alloc] initWithNibName:@"FertilizerEditViewController" bundle:nil];
            [self.navigationController pushViewController:viewController animated:YES];
            break;
        case 3:
            [self addDrugInf];
            [self.tblData reloadData];
            viewController = [[DrugEditViewController alloc] initWithNibName:@"DrugEditViewController" bundle:nil];
            [self.navigationController pushViewController:viewController animated:YES];
            break;

        default:
            break;
    }
    [self.dataList addObject:@"new"];
}

- (void)addDrugInf
{
    if (self.subDataList4.count == 0)
    {
        PlanEditBean *elem = [[PlanEditBean alloc] init];
        elem.name = @"肥料种类:";
        elem.value = @"化肥";
        [self.subDataList4 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"品牌:";
        elem.value = @"德美亚1号";
        [self.subDataList4 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"用量:";
        elem.value = @"200市斤/公顷";
        [self.subDataList4 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"期望价格:";
        elem.value = @"1300元/公顷";
        [self.subDataList4 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"施肥次序:";
        elem.value = @"一追";
        [self.subDataList4 addObject:elem];
    }
}

- (void)addFertilizerInf
{
    if (self.subDataList3.count == 0)
    {
        PlanEditBean *elem = [[PlanEditBean alloc] init];
        elem.name = @"肥料种类:";
        elem.value = @"化肥";
        [self.subDataList3 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"品牌:";
        elem.value = @"德美亚1号";
        [self.subDataList3 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"用量:";
        elem.value = @"200市斤/公顷";
        [self.subDataList3 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"期望价格:";
        elem.value = @"1300元/公顷";
        [self.subDataList3 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"施肥次序:";
        elem.value = @"一追";
        [self.subDataList3 addObject:elem];
    }
}

- (void)addSeedInf
{
    if (self.subDataList2.count == 0)
    {
        PlanEditBean *elem = [[PlanEditBean alloc] init];
        elem.name = @"厂家:";
        elem.value = @"厂家111";
        [self.subDataList2 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"种植品种:";
        elem.value = @"德美亚1号";
        [self.subDataList2 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"用量:";
        elem.value = @"200市斤/公顷";
        [self.subDataList2 addObject:elem];
    }
}

- (void)addBlockInf
{
    if (self.subDataList1.count == 0)
    {
        PlanEditBean *elem = [[PlanEditBean alloc] init];
        elem.name = @"计划编号:";
        elem.value = @"000001";
        [self.subDataList1 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"地块信息:";
        elem.value = @"北坡－转包－私人－100公顷";
        [self.subDataList1 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"种植面积:";
        elem.value = @"200公顷";
        [self.subDataList1 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"作物种类:";
        elem.value = @"玉米";
        [self.subDataList1 addObject:elem];
        elem = [[PlanEditBean alloc] init];
        elem.name = @"种植时间:";
        elem.value = @"2016-09-11";
        [self.subDataList1 addObject:elem];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.tblData reloadData];
}

#pragma mark 排序 当移动了某一行时候会调用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 32;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return self.sectionNameList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *array = self.dataList[section];
    return array.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 78; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlanEditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlanEditCell"];
    long section = indexPath.section;
    long row = indexPath.row;
    NSMutableArray *array = self.dataList[section];
    PlanEditBean *elem = array[row];
    cell.lblName.text = elem.name;
    cell.lblValue.text = elem.value;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 52;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view;
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 52)];
    view.backgroundColor = [LPCTool colorWithHexString:COLOR_APP_BG];

    UIView *subView;
    subView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, tableView.frame.size.width, 32)];
    subView.backgroundColor = [UIColor whiteColor];

    UILabel *label;
    label = [[UILabel alloc] initWithFrame:CGRectMake(32, 0, tableView.frame.size.width - 100, 32)];
    label.font = [UIFont boldSystemFontOfSize:16.0];
    label.text = self.sectionNameList[section];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(6, 6, 20, 20)];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 60, 0, 60, 32)];
    //如果是type是99 农民用户无法编辑
    if (section < 2 )
    {
        [button setTitle:@"编辑" forState:UIControlStateNormal];
    }
    else
    {
        [button setTitle:@"添加" forState:UIControlStateNormal];
    }
    switch (section)
    {
        case 0:
            label.textColor = [LPCTool colorWithHexString:COLOR_APP_GREEN];
            imageView.image = [UIImage imageNamed:@"05-earth"];
            break;
        case 1:
            label.textColor = [LPCTool colorWithHexString:COLOR_APP_ORANGE];
            imageView.image = [UIImage imageNamed:@"05-seed"];
            break;
        case 2:
            label.textColor = [LPCTool colorWithHexString:COLOR_APP_BLUE];
            imageView.image = [UIImage imageNamed:@"05-fertilizer"];
            break;
        case 3:
            label.textColor = [LPCTool colorWithHexString:COLOR_APP_LIGHT_BLUE];
            imageView.image = [UIImage imageNamed:@"05-medicine"];
            break;

        default:
            break;
    }

    [button setTitle:@"编辑" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.tag = section;
    [button addTarget:self action:@selector(doClickNew:) forControlEvents:UIControlEventTouchUpInside];

    if (self.type != 99) {
        [subView addSubview:button];
    }
    [subView addSubview:label];
    [subView addSubview:imageView];

    [view addSubview:subView];
    return view;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)doCommit:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
